﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.TextMining.FullTextSearch
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class LuceneIndexingInstruction:InstructionBase
    {
        #region props
        private string _ConnStr;
        /// <summary></summary>
        public string ConnStr
        {
            get
            {
                return this._ConnStr;
            }
            set
            {
                this._ConnStr = value;
            }
        }

        private string _SqlStatement;
        /// <summary></summary>
        public string SqlStatement
        {
            get
            {
                return this._SqlStatement;
            }
            set
            {
                this._SqlStatement = value;
            }
        }

        private string _KeyField;
        /// <summary></summary>
        public string KeyField
        {
            get
            {
                return this._KeyField;
            }
            set
            {
                this._KeyField = value;
            }
        }

        private List<string> _ContentFields;
        /// <summary></summary>
        public List<string> ContentFields
        {
            get
            {
                return this._ContentFields;
            }
            set
            {
                this._ContentFields = value;
            }
        }

        private string _IndexingOutputFilePath;
        /// <summary></summary>
        public string IndexingOutputFilePath
        {
            get
            {
                return this._IndexingOutputFilePath;
            }
            set
            {
                this._IndexingOutputFilePath = value;
            }
        }
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public LuceneIndexingInstruction():base(Guid.Empty,Guid.Empty)
        {
            this._ConnStr = string.Empty;
            this._SqlStatement = string.Empty;
            this._KeyField = string.Empty;
            this._ContentFields=new List<string>();
            this._IndexingOutputFilePath = string.Empty;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="componentID"></param>
        /// <param name="jobTicketID"></param>
        /// <param name="connStr"></param>
        /// <param name="sqlStatement"></param>
        /// <param name="keyField"></param>
        /// <param name="contentFields"></param>
        /// <param name="outputFilePath"></param>
        public LuceneIndexingInstruction(Guid componentID, Guid jobTicketID,
            string connStr, string sqlStatement, 
            string keyField, List<string> contentFields,
            string outputFilePath) 
            : base(componentID, jobTicketID)
        {
            this._ConnStr = connStr;
            this._SqlStatement = sqlStatement;
            this._KeyField = keyField;
            this._ContentFields = contentFields;
            this._IndexingOutputFilePath = outputFilePath;
        }
        #endregion

        #region Overrides of InstructionBase
        /// <summary>
        /// serialize settings to xml
        /// the function below is mainly for storing into database as serialized form instead of string
        /// </summary>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Instruction");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ComponentID", this.OwnerComponentID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SubmitTime", this.SubmitTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "StartTime", this.StartTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ConnStr",this.ConnStr);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Sql",this.SqlStatement);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "KeyField",this.KeyField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "OutputFilePath",this.IndexingOutputFilePath);
            if(this.ContentFields !=null && this.ContentFields.Count>0)
            {
                XmlNode fieldNodes = XmlDataUtil.AddElement(ref xDoc, xNode, "Fields");
                foreach(string field in this.ContentFields)
                {
                    XmlNode fieldNode = XmlDataUtil.AddElement(ref xDoc, fieldNodes, "Field");
                    XmlDataUtil.UpdateAttribute(ref xDoc, fieldNode,"FieldName",field);
                }
            }

            return xNode;
        }

        /// <summary>
        /// instantiated from xml
        /// "&lt;Instructions&gt;&lt;Instruction&gt;&lt;/Instruction&gt;&lt;/Instruction&gt;", must
        /// instantiate from first child of root node
        /// </summary>
        public override InstructionBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                LuceneIndexingInstruction instruction = new LuceneIndexingInstruction();
                instruction.OwnerComponentID = new Guid(XmlDataUtil.GetAttributeValue(xNode, "ComponentID", Guid.NewGuid().ToString()));
                instruction.OwnerWorkerInstanceID = new Guid(XmlDataUtil.GetAttributeValue(xNode, "WorkerID", Guid.NewGuid().ToString()));
                instruction.SubmitTime = DateTime.Parse(XmlDataUtil.GetAttributeValue(xNode, "SubmitTime", TimeUtil.DbMinTime.ToString()));
                instruction.StartTime = DateTime.Parse(XmlDataUtil.GetAttributeValue(xNode, "StartTime", TimeUtil.DbMinTime.ToString()));

                instruction.ConnStr = XmlDataUtil.GetAttributeValue(xNode, "ConnStr", "");
                instruction.SqlStatement = XmlDataUtil.GetAttributeValue(xNode, "Sql", "");
                instruction.KeyField = XmlDataUtil.GetAttributeValue(xNode, "KeyField", "");
                instruction.IndexingOutputFilePath = XmlDataUtil.GetAttributeValue(xNode, "OutputFilePath", "");
                XmlNodeList fieldNodes = xNode.SelectNodes("Fields/Field");
                if(fieldNodes !=null && fieldNodes.Count>0)
                {
                    instruction.ContentFields=new List<string>();
                    foreach(XmlNode fieldNode in fieldNodes)
                    {
                        instruction.ContentFields.Add(XmlDataUtil.GetAttributeValue(fieldNode, "FieldName", ""));
                    }
                }

                return instruction;
            }
            else
            {
                throw new Exception("Unable to instantiate component: invalid type specified in xml node");
            }
        }

        #endregion
    }
}
